/*需求：(实现一个自定义的vector)
使用模板类实现一个动态数组类，
该类能存放任何类型的对象数组，并有增删查改相关函数.
(参考课件PPT上资料，网校视频)*/
#include <iostream>
using namespace std;
#include <cstring>
 
template <typename T>
class myVector{
private:
	T*		m_data;			
	int 	m_capacity;		
	int 	m_size;		
public:	
	typedef T* iterator;

	iterator begin() { return this->m_data; }
	iterator end() { return this->m_data + this->m_size; }

	myVector(int n, const T& value);			
	myVector(const myVector& x);				

	myVector(int maxSize = 5);
	~myVector();

	void push_back(const T& value);
	void pop_back();

	int size() const { return this->m_size; }

	T& operator [](int index);

    void erase(int position);
};

//构造函数
template <typename T>
myVector<T>::myVector(int maxSize)
	:m_size(0),
	m_capacity(maxSize)
{
	this->m_data = new T[this->m_capacity]();
}
//析构函数
template <typename T>
myVector<T>::~myVector()
{
	if(this->m_data)
    {
        delete [] this->m_data;
    }
}
//插在最后
template <typename T>
void myVector<T>::push_back(const T& value)
{
	if(this->m_size >= this->m_capacity){
		this->m_capacity += this->m_capacity / 2;
		T* _tmp = new T[this->m_capacity]();
		for(int i = 0; i < this->m_size; i++)
        {
			_tmp[i] = this->m_data[i];
		}
		delete [] this->m_data;
		this->m_data  = _tmp;
	}
	this->m_data[m_size++] = value;
}
//删除最后
template <typename T>
void myVector<T>::pop_back()
{
	m_size--;
}
//重载[]  修改
template <typename T>
T& myVector<T>::operator [] (int index)
{
	return this->m_data[index];
}
//删除指定位置
template <typename T>
void myVector<T>::erase(int position)
{
    if(position<0 || position>m_size)
    {
        return;
    }
    for(int i=position;i<m_size-1;i++)
    {
        T tmp;
        tmp=m_data[i];
        m_data[i]=m_data[i+1];
        m_data[i+1]=tmp;
        m_size--;
    }
}

int main(int argc,char*argv[])
{
    myVector<int> a;
    for(int i=0;i<10;i++)
    {
        a.push_back(i);
    }
    a.pop_back();
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    a[3]=23;
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

